package com.example.covid19.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.covid19.common.Dto.Editdto;
import com.example.covid19.common.lang.Result;
import com.example.covid19.entity.Enum.MessageTemplate;
import com.example.covid19.entity.StationMessage;
import com.example.covid19.entity.Student;
import com.example.covid19.service.StationMessageService;
import com.example.covid19.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@Slf4j
@Component
@RequestMapping("util")
public class MessageUtil implements Set {
    @Autowired
    StationMessageService stationMessageService;
    @Autowired
    StudentService studentService;
    /**
     * @return 对所有用户进行发送站内信,这个是每日打卡通知 biaoshi决定每日打卡和发送换寝请求
     * 0每日打卡1寝室 这里得定时任务记得给参数，不然无法发送
     */
    @RequestMapping("sendAny")
    public Result sendAny(int i,int rid,String uid) {
        List<StationMessage> stationMessageList = new ArrayList<>();
        switch (i){
            case 0: studentService.list().forEach(v->stationMessageList.add(set(v.getStudentid(), 1)));break;
            case 1: studentService.list(new QueryWrapper<Student>().eq("rid",rid)).forEach(v->{
                stationMessageList.add(set2(v.getStudentid(), 2,uid));
            });break;
        }
        stationMessageService.saveBatch(stationMessageList);
        return Result.success(200,"成功",null);
    }

    /**
     *
     * @param
     * @return 发送学生单条站内信,根据标识进行发送不同类型的信息
     */
    @Override
    public Result sendone(String studentid,int i){
        List<StationMessage>stationMessageList = new ArrayList<>();
        switch (i){
            case 2:set(Integer.valueOf(studentid),2);break;
            case 3:set(Integer.valueOf(studentid),3);break;
            case 4:set(Integer.valueOf(studentid),4);break;
            case 5:set(Integer.valueOf(studentid),5);break;
            case 6:set(Integer.valueOf(studentid),6);break;
        }
        return Result.success(200,"成功",null);
    }

    /**
     *
     * @param studentid
     * @param i
     * @param auid
     * @return这个地方是发送换寝室的
     */
    @Override
    public Result sendone(String studentid,int i,Integer auid){
        switch (i){
            case 1:set(Integer.valueOf(studentid),7,String.valueOf(auid));break;
//            case 3:set(Integer.valueOf(studentid),3);break;
        }
        return Result.success(200,"成功",null);
    }
    public Result sendtwo(String studentid,int i,Integer auid){
        switch (i){
            case 1:set(Integer.valueOf(studentid),9,String.valueOf(auid));break;
//            case 3:set(Integer.valueOf(studentid),3);break;
        }
        return Result.success(200,"成功",null);
    }
    @Override
    public StationMessage set(Integer sid, Integer templateId) {
        StationMessage stationMessage = new StationMessage();
        stationMessage.setState(0);
        stationMessage.setStudentid(sid);
        stationMessage.setContent(MessageTemplate.map.get(templateId).getContent());
        stationMessage.setTitle(MessageTemplate.map.get(templateId).getTitle());
        stationMessage.setTimeer(MessageTemplate.map.get(templateId).getSendTime());
        stationMessage.setExpirationTime(MessageTemplate.map.get(templateId).getExpirationTime());
        stationMessage.setTemplate(MessageTemplate.map.get(templateId).getTemp());
        stationMessageService.save(stationMessage);
        return stationMessage;
    }
    @Override
    public StationMessage set(Integer sid, Integer templateId, String auid) {
        StationMessage stationMessage = new StationMessage();
        stationMessage.setState(0);
        stationMessage.setStudentid(sid);
        stationMessage.setAuid(Integer.valueOf(auid));
        stationMessage.setContent(MessageTemplate.map.get(templateId).getContent());
        stationMessage.setTitle(MessageTemplate.map.get(templateId).getTitle());
        stationMessage.setTimeer(MessageTemplate.map.get(templateId).getSendTime());
        stationMessage.setExpirationTime(MessageTemplate.map.get(templateId).getExpirationTime());
        stationMessage.setTemplate(MessageTemplate.map.get(templateId).getTemp());
        stationMessageService.save(stationMessage);
        return stationMessage;
    }
    public StationMessage set2(Integer sid, Integer templateId,String uid) {
        StationMessage stationMessage = new StationMessage();
        stationMessage.setState(0);
        stationMessage.setStudentid(sid);
        stationMessage.setUid(uid);
        stationMessage.setContent(MessageTemplate.map.get(templateId).getContent());
        stationMessage.setTitle(MessageTemplate.map.get(templateId).getTitle());
        stationMessage.setTimeer(MessageTemplate.map.get(templateId).getSendTime());
        stationMessage.setExpirationTime(MessageTemplate.map.get(templateId).getExpirationTime());
        stationMessage.setTemplate(MessageTemplate.map.get(templateId).getTemp());
        return stationMessage;
    }
}

